#!/bin/env python

# code2blog, a pyGTK front-end to GNU/Source Highlight
# Copyright (C) 2006 Jim Storch

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import pango
import gobject
import subprocess
import os.path

gladetext="""
<glade-interface>

<widget class="GtkWindow" id="main_window">
  <property name="width_request">800</property>
  <property name="height_request">600</property>
  <property name="visible">True</property>
  <property name="title" translatable="yes">code2blog</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
	<widget class="GtkToolbar" id="toolbar1">
	  <property name="visible">True</property>
	  <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
	  <property name="toolbar_style">GTK_TOOLBAR_BOTH</property>
	  <property name="tooltips">True</property>
	  <property name="show_arrow">True</property>

	  <child>
	    <widget class="GtkToolButton" id="button_open_input">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Open a source code file</property>
	      <property name="stock_id">gtk-open</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_open_input_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:24 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_paste_input">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Paste input from the clipboard</property>
	      <property name="stock_id">gtk-paste</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_paste_input_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:32 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_clear_input">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Clear the input text area</property>
	      <property name="stock_id">gtk-clear</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_clear_input_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:37 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem4">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_apply">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Run GNU/Source Highlight on the input</property>
	      <property name="stock_id">gtk-apply</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_apply_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:17 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem3">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_save_output">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Save the output to a text file</property>
	      <property name="stock_id">gtk-save</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_save_output_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:46 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_copy_output">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Copy the output to the system clipboard</property>
	      <property name="stock_id">gtk-copy</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_copy_output_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:52 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_clear_output">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Clear the output text area</property>
	      <property name="stock_id">gtk-clear</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_clear_output_clicked" last_modification_time="Thu, 14 Sep 2006 16:01:59 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem2">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_about">
	      <property name="visible">True</property>
	      <property name="stock_id">gtk-about</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_about_clicked" last_modification_time="Fri, 15 Sep 2006 15:01:05 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem1">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolButton" id="button_quit">
	      <property name="visible">True</property>
	      <property name="tooltip" translatable="yes">Exit this program</property>
	      <property name="stock_id">gtk-quit</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>
	      <signal name="clicked" handler="on_button_quit_clicked" last_modification_time="Thu, 14 Sep 2006 16:02:04 GMT"/>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">True</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>

      <child>
	<widget class="GtkToolbar" id="toolbar2">
	  <property name="visible">True</property>
	  <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
	  <property name="toolbar_style">GTK_TOOLBAR_BOTH</property>
	  <property name="tooltips">True</property>
	  <property name="show_arrow">True</property>

	  <child>
	    <widget class="GtkToolItem" id="toolitem1">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkLabel" id="label3">
		  <property name="visible">True</property>
		  <property name="label" translatable="yes">Source Language </property>
		  <property name="use_underline">False</property>
		  <property name="use_markup">False</property>
		  <property name="justify">GTK_JUSTIFY_LEFT</property>
		  <property name="wrap">False</property>
		  <property name="selectable">False</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xpad">0</property>
		  <property name="ypad">0</property>
		  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
		  <property name="width_chars">-1</property>
		  <property name="single_line_mode">False</property>
		  <property name="angle">0</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem2">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkComboBox" id="combo_input_format">
		  <property name="visible">True</property>
		  <property name="add_tearoffs">False</property>
		  <property name="focus_on_click">True</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem5">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem4">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkLabel" id="label4">
		  <property name="visible">True</property>
		  <property name="label" translatable="yes">Output Format </property>
		  <property name="use_underline">False</property>
		  <property name="use_markup">False</property>
		  <property name="justify">GTK_JUSTIFY_LEFT</property>
		  <property name="wrap">False</property>
		  <property name="selectable">False</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xpad">0</property>
		  <property name="ypad">0</property>
		  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
		  <property name="width_chars">-1</property>
		  <property name="single_line_mode">False</property>
		  <property name="angle">0</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem5">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkComboBox" id="combo_output_format">
		  <property name="visible">True</property>
		  <property name="add_tearoffs">False</property>
		  <property name="focus_on_click">True</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem6">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem7">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkLabel" id="label5">
		  <property name="visible">True</property>
		  <property name="label" translatable="yes">Tab Size</property>
		  <property name="use_underline">False</property>
		  <property name="use_markup">False</property>
		  <property name="justify">GTK_JUSTIFY_LEFT</property>
		  <property name="wrap">False</property>
		  <property name="selectable">False</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xpad">0</property>
		  <property name="ypad">0</property>
		  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
		  <property name="width_chars">-1</property>
		  <property name="single_line_mode">False</property>
		  <property name="angle">0</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem8">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkSpinButton" id="spinner_tab_size">
		  <property name="visible">True</property>
		  <property name="tooltip" translatable="yes">Convert tab characters in the source to this many spaces</property>
		  <property name="can_focus">True</property>
		  <property name="climb_rate">1</property>
		  <property name="digits">0</property>
		  <property name="numeric">True</property>
		  <property name="update_policy">GTK_UPDATE_ALWAYS</property>
		  <property name="snap_to_ticks">False</property>
		  <property name="wrap">False</property>
		  <property name="adjustment">4 1 12 1 10 10</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkSeparatorToolItem" id="separatortoolitem7">
	      <property name="visible">True</property>
	      <property name="draw">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkToolItem" id="toolitem14">
	      <property name="visible">True</property>
	      <property name="visible_horizontal">True</property>
	      <property name="visible_vertical">True</property>
	      <property name="is_important">False</property>

	      <child>
		<widget class="GtkCheckButton" id="check_number">
		  <property name="visible">True</property>
		  <property name="tooltip" translatable="yes">Add line numbers to the output?</property>
		  <property name="can_focus">True</property>
		  <property name="label" translatable="yes">Number</property>
		  <property name="use_underline">True</property>
		  <property name="relief">GTK_RELIEF_NORMAL</property>
		  <property name="focus_on_click">True</property>
		  <property name="active">False</property>
		  <property name="inconsistent">False</property>
		  <property name="draw_indicator">True</property>
		</widget>
	      </child>
	    </widget>
	    <packing>
	      <property name="expand">False</property>
	      <property name="homogeneous">False</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>

      <child>
	<widget class="GtkHPaned" id="hpaned1">
	  <property name="visible">True</property>
	  <property name="can_focus">True</property>

	  <child>
	    <widget class="GtkFrame" id="frame1">
	      <property name="visible">True</property>
	      <property name="label_xalign">0</property>
	      <property name="label_yalign">0.5</property>
	      <property name="shadow_type">GTK_SHADOW_NONE</property>

	      <child>
		<widget class="GtkAlignment" id="alignment1">
		  <property name="visible">True</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xscale">1</property>
		  <property name="yscale">1</property>
		  <property name="top_padding">0</property>
		  <property name="bottom_padding">0</property>
		  <property name="left_padding">12</property>
		  <property name="right_padding">0</property>

		  <child>
		    <widget class="GtkScrolledWindow" id="scrolledwindow1">
		      <property name="visible">True</property>
		      <property name="can_focus">True</property>
		      <property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
		      <property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
		      <property name="shadow_type">GTK_SHADOW_IN</property>
		      <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

		      <child>
			<widget class="GtkTextView" id="input_view">
			  <property name="visible">True</property>
			  <property name="can_focus">True</property>
			  <property name="editable">True</property>
			  <property name="overwrite">False</property>
			  <property name="accepts_tab">True</property>
			  <property name="justification">GTK_JUSTIFY_LEFT</property>
			  <property name="wrap_mode">GTK_WRAP_NONE</property>
			  <property name="cursor_visible">True</property>
			  <property name="pixels_above_lines">0</property>
			  <property name="pixels_below_lines">0</property>
			  <property name="pixels_inside_wrap">0</property>
			  <property name="left_margin">0</property>
			  <property name="right_margin">0</property>
			  <property name="indent">0</property>
			  <property name="text" translatable="yes"></property>
			</widget>
		      </child>
		    </widget>
		  </child>
		</widget>
	      </child>

	      <child>
		<widget class="GtkLabel" id="label1">
		  <property name="visible">True</property>
		  <property name="label" translatable="yes">&lt;b&gt;Input&lt;/b&gt;</property>
		  <property name="use_underline">False</property>
		  <property name="use_markup">True</property>
		  <property name="justify">GTK_JUSTIFY_LEFT</property>
		  <property name="wrap">False</property>
		  <property name="selectable">False</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xpad">0</property>
		  <property name="ypad">0</property>
		  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
		  <property name="width_chars">-1</property>
		  <property name="single_line_mode">False</property>
		  <property name="angle">0</property>
		</widget>
		<packing>
		  <property name="type">label_item</property>
		</packing>
	      </child>
	    </widget>
	    <packing>
	      <property name="shrink">True</property>
	      <property name="resize">True</property>
	    </packing>
	  </child>

	  <child>
	    <widget class="GtkFrame" id="frame2">
	      <property name="visible">True</property>
	      <property name="label_xalign">0</property>
	      <property name="label_yalign">0.5</property>
	      <property name="shadow_type">GTK_SHADOW_NONE</property>

	      <child>
		<widget class="GtkAlignment" id="alignment2">
		  <property name="visible">True</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xscale">1</property>
		  <property name="yscale">1</property>
		  <property name="top_padding">0</property>
		  <property name="bottom_padding">0</property>
		  <property name="left_padding">12</property>
		  <property name="right_padding">0</property>

		  <child>
		    <widget class="GtkScrolledWindow" id="scrolledwindow2">
		      <property name="visible">True</property>
		      <property name="can_focus">True</property>
		      <property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
		      <property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
		      <property name="shadow_type">GTK_SHADOW_IN</property>
		      <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

		      <child>
			<widget class="GtkTextView" id="output_view">
			  <property name="visible">True</property>
			  <property name="can_focus">True</property>
			  <property name="editable">True</property>
			  <property name="overwrite">False</property>
			  <property name="accepts_tab">True</property>
			  <property name="justification">GTK_JUSTIFY_LEFT</property>
			  <property name="wrap_mode">GTK_WRAP_NONE</property>
			  <property name="cursor_visible">True</property>
			  <property name="pixels_above_lines">0</property>
			  <property name="pixels_below_lines">0</property>
			  <property name="pixels_inside_wrap">0</property>
			  <property name="left_margin">0</property>
			  <property name="right_margin">0</property>
			  <property name="indent">0</property>
			  <property name="text" translatable="yes"></property>
			</widget>
		      </child>
		    </widget>
		  </child>
		</widget>
	      </child>

	      <child>
		<widget class="GtkLabel" id="label2">
		  <property name="visible">True</property>
		  <property name="label" translatable="yes">&lt;b&gt;Output&lt;/b&gt;</property>
		  <property name="use_underline">False</property>
		  <property name="use_markup">True</property>
		  <property name="justify">GTK_JUSTIFY_LEFT</property>
		  <property name="wrap">False</property>
		  <property name="selectable">False</property>
		  <property name="xalign">0.5</property>
		  <property name="yalign">0.5</property>
		  <property name="xpad">0</property>
		  <property name="ypad">0</property>
		  <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
		  <property name="width_chars">-1</property>
		  <property name="single_line_mode">False</property>
		  <property name="angle">0</property>
		</widget>
		<packing>
		  <property name="type">label_item</property>
		</packing>
	      </child>
	    </widget>
	    <packing>
	      <property name="shrink">True</property>
	      <property name="resize">True</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkStatusbar" id="statusbar1">
	  <property name="visible">True</property>
	  <property name="has_resize_grip">True</property>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
"""

INPUT_LIST = [
	"bison",
	"c",
	"cpp",
	"caml",
	"changelog",
	"diff",
	"eps",
	"flex",
	"html",
	"java",
	"javascript",
	"latex",
	"logtalk",
	"lua",
	"pascal",
	"perl",
	"postscript",
	"python",
	"ruby",
	"sml",
	"style",
	]

OUTPUT_LIST = [
	"docbook",
	"esc",
	"html",
	"html-css",
	"html-css-doc",
	"html-doc",
	"javadoc",
	"latex",
	"latex-doc",
	"latexcolor",
	"latexcolor-doc",
	"texinfo",
	"xhtml",
	"xhtml-css",
	"xhtml-css-doc",
	"xhtml-doc",
	]

def dependencyCheck():
	try:
		version = subprocess.Popen(
			['source-highlight',"--version"],
			stdout=subprocess.PIPE).communicate()[0]
	except OSError:
		return False  
	else:
		return True

def colorize(text, lang, format, tab_size, line_numbers=False):
	cmd = ["source-highlight",
		"--src-lang", lang,
		"--out-format", format,
		"--tab", tab_size,]

	if line_numbers:
		cmd.append("--line-number-ref")

	child = subprocess.Popen(cmd,
		stdin=subprocess.PIPE,
		stdout=subprocess.PIPE)
	child.stdin.write(text)
	child.stdin.close()
	html = child.stdout.read()
	return html

	
class MyApp(object):

	def __init__(self):

		# Load our GUI from a glade file
		#self.gladefile = "code2blog.glade"  
		#self.wTree = gtk.glade.XML(self.gladefile)

		# load our GUI from a string instead	
		self.wTree = gtk.glade.xml_new_from_buffer (
			gladetext,len(gladetext),'main_window')
		self.window = self.wTree.get_widget("main_window")
		self.window.connect("destroy", gtk.main_quit)	

		# Map signals to methods
		dic = { 
			"on_button_open_input_clicked" : self.button_open_input_clicked,
			"on_button_paste_input_clicked" : self.button_paste_input_clicked,
			"on_button_clear_input_clicked": self.button_clear_input_clicked,
			"on_button_apply_clicked" : self.button_apply_clicked,
			"on_button_save_output_clicked" : self.button_save_output_clicked,
			"on_button_copy_output_clicked" : self.button_copy_output_clicked,
			"on_button_clear_output_clicked" : 
				self.button_clear_output_clicked,
			"on_button_about_clicked" : self.button_about_clicked,
			"on_button_quit_clicked" : self.button_quit_clicked,			
			"on_window1_destroy" : gtk.main_quit }
			
		self.wTree.signal_autoconnect(dic)
		
		self.input_view = self.wTree.get_widget("input_view")
		self.input_buffer = self.input_view.get_buffer()
		self.output_view = self.wTree.get_widget("output_view")
		self.output_buffer = self.output_view.get_buffer()

		# Change the TextView font to something monospacey
		self.font_desc = pango.FontDescription('Bitstream Vera Sans Mono')
		self.font_desc.set_size(9000)
		self.input_view.modify_font(self.font_desc)
		self.output_view.modify_font(self.font_desc)

		# populate the input format combo and default to python
		store = gtk.ListStore(gobject.TYPE_STRING)
		for format in INPUT_LIST:
			store.append ([format])
		self.input_format = self.wTree.get_widget("combo_input_format")
		self.input_format.set_model(store)
		cell = gtk.CellRendererText()
		self.input_format.pack_start(cell, True)
		self.input_format.add_attribute(cell, 'text',0)
		self.input_format.set_active(17)

		# populate the output format combo and default to HTML
		store = gtk.ListStore(gobject.TYPE_STRING)
		for format in OUTPUT_LIST:
			store.append ([format])
		self.output_format = self.wTree.get_widget("combo_output_format")
		self.output_format.set_model(store)
		cell = gtk.CellRendererText()
		self.output_format.pack_start(cell, True)
		self.output_format.add_attribute(cell, 'text',0)
		self.output_format.set_active(2)

		# Controls to tab size and whether to show line numbers		
		self.tab_size = self.wTree.get_widget("spinner_tab_size")
		self.check_number = self.wTree.get_widget("check_number")
		self.clipboard = gtk.Clipboard()
		self.last_filename = "new"
		self.last_format = "html"
		
	def button_open_input_clicked(self, widget):
		chooser = gtk.FileChooserDialog(
			title="Open Source File",action=gtk.FILE_CHOOSER_ACTION_OPEN,
			buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
			gtk.STOCK_OPEN,gtk.RESPONSE_OK))
		response = chooser.run()

		if response == gtk.RESPONSE_OK:
			filename = chooser.get_filename()
			file_object = open(filename,'r')
			try:
				contents = file_object.read()
			finally:
				file_object.close()
			self.input_buffer.set_text(contents)	

		chooser.destroy()		

	def button_paste_input_clicked(self, widget):
		self.input_buffer.paste_clipboard(
			self.clipboard, None, self.input_view.get_editable())
		
	def button_clear_input_clicked(self, widget):
		self.input_buffer.set_text("")

	def button_apply_clicked(self, widget):
		start, end = self.input_buffer.get_bounds()
		src = self.input_buffer.get_text(start, end)
		tabs = str(int(self.tab_size.get_value()))
		lang = self.input_format.get_active_text()
		format = self.output_format.get_active_text()
		show_line_numbers=self.check_number.get_active()
		output = colorize(src, lang=lang, format=format,
			tab_size=tabs, line_numbers=show_line_numbers)
		self.output_buffer.set_text(output)
		start, end = self.output_buffer.get_bounds()
		self.output_buffer.select_range(start, end)		

	def button_save_output_clicked(self, widget):
		chooser = gtk.FileChooserDialog(
			title="Save Output",action=gtk.FILE_CHOOSER_ACTION_SAVE,
			buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
			gtk.STOCK_SAVE,gtk.RESPONSE_OK))
		response = chooser.run()
	
		if response == gtk.RESPONSE_OK:
			filename = chooser.get_filename()
			file_exists = False
			file_overwrite = False
  	
  			if os.path.isfile(filename):
  				# if the file already exists
				file_exists = True
				warning_message = (
					"Are you sure you want to overwrite:\n" + 
					filename +
					"\nwith the contents of the OUTPUT window?\n\n"
					"Think before you click." ) 
				warning = gtk.MessageDialog(self.window,
					gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, 
					gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, warning_message)
				warning.add_button(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL)
				warning.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
	
				if warning.run() == gtk.RESPONSE_OK:
					# exists, but user said ok to overwrite
					file_overwrite = True
  				warning.destroy()
  	
  			if (file_exists == False) or ( file_exists == True and
				file_overwrite == True ):
				file_object = open(filename,'w')
				start, end = self.output_buffer.get_bounds()
				contents = self.output_buffer.get_text(start, end)
				try:
					file_object.write(contents)
				finally:
					file_object.close()
	
		chooser.destroy()

	def button_copy_output_clicked(self, widget):
		self.output_buffer.copy_clipboard(self.clipboard)

	def button_clear_output_clicked(self, widget):
		self.output_buffer.set_text("")	
		
	def button_about_clicked(self, widget):
			text = (
				"code2blog v.10\n"
				"A pyGTK front-end to GNU/Source Highlight\n"
				"Copyright (C) 2006 Jim Storch\n\n"
				"This is free software licensed under the GPL.\n\n"
				"Special thanks to Lorenzo Bettini who did all the\n"
				"heavy lifting by writing GNU/Source Highlight\n"
				"http://www.gnu.org/software/src-highlite"
				)
			about = gtk.MessageDialog(self.window,
				gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, 
				gtk.MESSAGE_INFO, gtk.BUTTONS_NONE, text)
			about.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
			about.run()
			about.destroy()

	def button_quit_clicked(self, widget):
		gtk.main_quit()

		
if __name__ == "__main__":
	if dependencyCheck():
		MyApp()
		gtk.main()
	else:
		print ( "Please install GNU/Source Highlight, "
			"http://www.gnu.org/software/src-highlite" )
